package com.csw.mapreduce;

//学生表和成绩表的合并


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


import java.io.IOException;
import java.util.ArrayList;


public class Demo04Join {

    public static class JoinMap extends Mapper<LongWritable,Text,Text,Text>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //判断是students.txt还是score.txt，通过路径判断
            //获取切片的路径
            FileSplit fs = (FileSplit) context.getInputSplit();
            //获取路径名称
            Path path = fs.getPath();
            String name = path.toString();
            if (name.contains("students.txt")){
                //以学号作为k  发送数据到reduce
                String id = value.toString().split(",")[0];
                //给学生信息打上标记
                String line="stu"+value.toString();
                context.write(new Text(id),new Text(line));
            }else{
                //以学号作为k  发送数据到reduce
                String id = value.toString().split(",")[0];
                //给学生成绩打上标记
                String line="sco"+value.toString();
                context.write(new Text(id),new Text(line));
            }
        }
    }
    public static class JoinReduce extends Reducer<Text,Text,Text,NullWritable>{
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            //一条students和多条score
            ArrayList<String> scores = new ArrayList<String>();
            String stuInFo="";
            for (Text value : values) {
                String s = value.toString();
                //获取去除标记的信息
                if (s.startsWith("stu")){
                    stuInFo= s.substring(3);
                }else{
                    scores.add(s.substring(19));
                }
            }
            //关联学生信息和学生成绩
            for (String score : scores) {
                    stuInFo=stuInFo+score;
            }
            context.write(new Text(stuInFo),NullWritable.get());
        }
    }
    public static void main(String[] args)throws Exception {
        Job job = Job.getInstance();//构建一个job
        job.setJobName("join");//指定job任务名称(不指定也可以 为系统默认的名称)
        job.setJarByClass(Demo04Join.class);//指定当前main所在类名

        //指定map类
        job.setMapperClass(JoinMap.class);
        //指定map类输出k和v的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        //指定reduce
        job.setReducerClass(JoinReduce.class);
        //指定reduce输出k和v的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        //指定输入路径读取两张表(students.txt,score.txt)
        FileInputFormat.addInputPath(job,new Path("/data/students.txt"));
        FileInputFormat.addInputPath(job,new Path("/data/score.txt"));

        //获取fileSystem对象，hdfs文件管理系统的对象
        FileSystem fs = FileSystem.get(new Configuration());
        Path path = new Path("/data/out3");
        //指定输出路径(路径不能已存在),如果已存在删除
        if (fs.exists(path)){
            fs.delete(path,true);
        }
        //指定输出路径
        FileOutputFormat.setOutputPath(job,path);
        //启动
        job.waitForCompletion(true);
    }
}
